package cc.yugu.yhdw.webserver.service.hdw;

import cc.yugu.yhdw.common.domain.hdw.CabinetStrategy;
import cc.yugu.yhdw.common.domain.hdw.ExchangeAmountMoneyDivide;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.common.entity.tree.Node;
import cc.yugu.yhdw.common.entity.tree.NodeModel;
import cc.yugu.yhdw.common.utils.YhdwUtils;
import cc.yugu.yhdw.webserver.persistence.hdw.ExchangeAmountMoneyDivideDetailMapper;
import cc.yugu.yhdw.webserver.persistence.hdw.ExchangeAmountMoneyDivideMapper;
import cc.yugu.yhdw.webserver.service.AbstractService;
import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

//import java.beans.Transient;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
/**
 * 换电次数金额划分业务
 * 
 * @Date:Created in 2018/7/9 10:03
 * @Author:
 * @Modified By: zjn 2018/7/9 10:03
 */ 
@Service
public class ExchangeAmountMoneyDivideService extends AbstractService {
    @Autowired
    ExchangeAmountMoneyDivideMapper exchangeAmountMoneyDivideMapper;
    @Autowired
    ExchangeAmountMoneyDivideDetailMapper exchangeAmountMoneyDivideDetailMapper;
    /**
    * 分页查询
    * 
    * @Param: [exchangeAmountMoneyDivide]
    * @Return: cc.yugu.yhdw.common.entity.pagination.Page
    * @Author By: zjn 2018/7/9  10:03
    */ 
    public Page findPage(ExchangeAmountMoneyDivide exchangeAmountMoneyDivide) {
        Page page = exchangeAmountMoneyDivide.buildPage();
        page.setTotalItems(exchangeAmountMoneyDivideMapper.findPageCount(exchangeAmountMoneyDivide));
        exchangeAmountMoneyDivide.setBeginIndex(page.getOffset());
        List<ExchangeAmountMoneyDivide> exchangeAmountMoneyDivideList = exchangeAmountMoneyDivideMapper.findPageResult(exchangeAmountMoneyDivide);
        page.setResult(exchangeAmountMoneyDivideList);

        return page;
    }
    /**
    * 根据主键查询表dw_exchange_amount_money_divide
    * 
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.domain.hdw.ExchangeAmountMoneyDivide
    * @Author By: zjn 2018/7/9  10:04
    */ 
    public ExchangeAmountMoneyDivide find(int id) {
        return exchangeAmountMoneyDivideMapper.find(id);
    }
    /**
    * 金额划分目录树
    * 
    * @Param: [agentId, agencyId, stream]
    * @Return: void
    * @Author By: zjn 2018/7/9  10:10
    */ 
    public void tree(Integer agentId, Integer agencyId, OutputStream stream) throws IOException {
        List<ExchangeAmountMoneyDivide> list =  exchangeAmountMoneyDivideMapper.findByAgent(agentId, agencyId);
        List<Node<NodeModel>> roots = new ArrayList<Node<NodeModel>>();
        for(ExchangeAmountMoneyDivide exchangeAmountMoneyDivide : list) {
            NodeModel nodeModel = new NodeModel();
            Node<NodeModel> root = new Node<NodeModel>(nodeModel);
            nodeModel.setId(exchangeAmountMoneyDivide.getId());
            nodeModel.setName(exchangeAmountMoneyDivide.getPlanName());
            roots.add(root);
        }

        ObjectMapper objectMapper = new ObjectMapper();
        JsonGenerator json = objectMapper.getJsonFactory().createJsonGenerator(stream, JsonEncoding.UTF8);
        json.writeStartArray();
        YhdwUtils.writeJson(roots, json);
        json.writeEndArray();
        json.flush();
        json.close();
    }
    /**
    * 查询
    * 
    * @Param: [agentId, agencyId]
    * @Return: java.util.List<cc.yugu.yhdw.common.domain.hdw.ExchangeAmountMoneyDivide>
    * @Author By: zjn 2018/7/9  10:11
    */ 
    public List<ExchangeAmountMoneyDivide> findByAgent(Integer agentId, Integer agencyId) {
        return exchangeAmountMoneyDivideMapper.findByAgent(agentId, agencyId);
    }
    /**
    * 按主键更新
    * 
    * @Param: [exchangeAmountMoneyDivide]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/9  10:11
    */ 
    public ExtResult update(ExchangeAmountMoneyDivide exchangeAmountMoneyDivide) {
        // 更新划分方案名称和备注
        int total = exchangeAmountMoneyDivideMapper.update(exchangeAmountMoneyDivide);
        if (total==0){
            return ExtResult.failResult("修改失败");
        }
        return ExtResult.successResult();
    }
    /**
    * 插入
    * 
    * @Param: [exchangeAmountMoneyDivide]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/9  10:12
    */ 
    public ExtResult insert(ExchangeAmountMoneyDivide exchangeAmountMoneyDivide) {
        int total = exchangeAmountMoneyDivideMapper.insert(exchangeAmountMoneyDivide);
        if (total==0){
            return ExtResult.failResult("新增失败");
        }
        return ExtResult.successResult();
    }
    /**
    * 删除
    * 
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/9  10:13
    */ 
    @Transactional(rollbackFor = Throwable.class)
    public ExtResult delete(int id) {
        int total = exchangeAmountMoneyDivideMapper.delete(id);
        if (total==0){
            return ExtResult.failResult("删除失败");
        }
        total = exchangeAmountMoneyDivideDetailMapper.deleteByDivideId(id);
        if (total==0){
            return ExtResult.failResult("删除失败");
        }
        return ExtResult.successResult();
    }

}
